<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>

<div id="projectPhaseDialog" title="选择项目和学期" style="display:none;">
<br/>
<label for="project">项目：</label>
<select id="project">
</select>
&nbsp;
<label for="semester">项目：</label>
<select id="semester">
</select>
</div>

<!--
资助跟踪几个列表界面，共用的项目、学期选择对话框，以及相应的js
对include这段共用代码的约定：
1. 有两个hidden input：selectedProjectId, selectedSemesterId
2. 存在filterRecords()这个js方法，调用后筛选符合条件的记录。这个方法接受一个参数：是否切换了项目或学期，为true的话，显示第一页。
-->

<!-- 这部分为公共js方法，本fragment和整个页面的js都可以使用 -->
<script type="text/javascript">
function fnAddSelectOption(objId, url, callback) {
	var objMessage = $("#txtMessage");
	$.ajax({
		type: "GET",
		url: url,
		success: function(resp) {
			if("0" == resp.code) {
				var objSelect = $(objId);
				$.each(resp.data, function(index, item) {
					var value = ("code" in item) ? item.code : item.id;
					var optionHtml = "<option value='" + value + "'>" + item.name + "</option>";
					objSelect.append(optionHtml);
				});
			} else {
				objMessage.prepend("<div>请求" + url + "返回出错信息：" + resp.message + "</div>")
			}
			if(callback) {
				callback();
			}
		},
		error: function () { objMessage.prepend("<div>请求" + url + " 服务器处理请求出错，请联系IT运维人员。</div>") }
	});
}

</script>

<script type="text/javascript">

var phaseDialog = $("#projectPhaseDialog").dialog({
    autoOpen: false,
    height:150,
    width: 320,
    modal: true,
	close: function() {}
});

function popupProjectPhaseDialog() {
	var projectOptions = $("#projectPhaseDialog #project").find("option");
	if(projectOptions.length < 1) {
		initProjectList();	// 初始化project list后，自动初始化 semester list
	}
	
	phaseDialog = phaseDialog.dialog({
	    buttons: {
	    	"确定": fnProjectPhaseSelected,
	    	Cancel: function() {
	    		phaseDialog.dialog("close");
	    	}
	    }
	});
	phaseDialog.dialog("open");
}

function fnProjectPhaseSelected() {
	var projectId = $("#projectPhaseDialog #project").val();
	var semesterId = $("#projectPhaseDialog #semester").val();
	
	if(!projectId || projectId.length==0 || !semesterId || semesterId.length==0) {
		alert("项目和学期都不能为空，请选择。");
		return;
	}
	
	$("#selectedProjectId").val(projectId);
	$("#selectedSemesterId").val(semesterId);
	
	var txtProjectPhase = " &nbsp; "; 
	txtProjectPhase += $("#projectPhaseDialog #project option:selected").text();
	txtProjectPhase += " " + $("#projectPhaseDialog #semester option:selected").text();
	$("#txtProjectPhase").html(txtProjectPhase);

	phaseDialog.dialog("close");
	
	
	filterRecords(true);
}

function initProjectList() {
	var projectListUrl = "<%=request.getContextPath() %>" + "/public/projects";
	fnAddSelectOption("#projectPhaseDialog #project", projectListUrl, initSemesterList);

	$("#projectPhaseDialog #project").change(function(){
		initSemesterList();
	});
}

function initSemesterList() {
	var projectId = $("#projectPhaseDialog #project").val();
	if(!projectId) {
		var firstOption = $("#projectPhaseDialog #project").find("option").first();
		firstOption.attr("selected", true);
		projectId = firstOption.val();
	}
	var availableSemetserUrl = "<%=request.getContextPath() %>" + "/projects/" + projectId + "/phases";
	$("#projectPhaseDialog #semester").empty();
	fnAddSelectOption("#projectPhaseDialog #semester", availableSemetserUrl);
	var semesterId = $("#selectedSemesterId").val();
	$("#projectPhaseDialog #semester").val(semester);
}

$(document).ready(function(){
	// nothing to do
});

</script>

